package com.mvp.lt.airlineview.picture;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;

import com.luck.picture.lib.compress.Checker;
import com.mvp.lt.airlineview.utils.LogUtil;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Responsible for starting compress and managing active and cached resources.
 */
public class PictureEngine {
    private String srcImg;
    private File tagImg;
    private int srcWidth;
    private int srcHeight;
    private long mSrcSize = 0;
    //压缩大小100kb
    private int mLeastCompressSize = 100;

    public File compressFile() throws IOException {
        File file = compress(srcImg, tagImg);
        long tagSize = file.length();
        if (Math.abs(mSrcSize - tagSize) > 100 * 1024) {
            if (tagSize > mLeastCompressSize * 1024) {
                mSrcSize = tagImg.length();
                srcImg = file.getAbsolutePath();
                tagImg = file;
                setOptions(file.getAbsolutePath());
                return compressFile();
            } else {
                return file;
            }
        } else {
            return file;
        }

    }

    public PictureEngine(String srcImg, File tagImg, int leastCompressSize) throws IOException {
        this.tagImg = tagImg;
        this.srcImg = srcImg;
        this.mLeastCompressSize = leastCompressSize;
        mSrcSize = new File(srcImg).length();
        setOptions(srcImg);
    }


    private void setOptions(String srcImg) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inSampleSize = 1;
        BitmapFactory.decodeFile(srcImg, options);
        this.srcWidth = options.outWidth;
        this.srcHeight = options.outHeight;
    }


    public File compress(String srcImg, File tagImg) throws IOException {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = computeSize();

        Bitmap tagBitmap = BitmapFactory.decodeFile(srcImg, options);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        tagBitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
        tagBitmap.recycle();

        FileOutputStream fos = new FileOutputStream(tagImg);
        fos.write(stream.toByteArray());
        fos.flush();
        fos.close();
        stream.close();

        LogUtil.e("压缩图片", "大小：" + tagImg.length());
        return tagImg;
    }

    private int computeSize() {
        srcWidth = srcWidth % 2 == 1 ? srcWidth + 1 : srcWidth;
        srcHeight = srcHeight % 2 == 1 ? srcHeight + 1 : srcHeight;

        int longSide = Math.max(srcWidth, srcHeight);
        int shortSide = Math.min(srcWidth, srcHeight);

        float scale = ((float) shortSide / longSide);
        if (scale <= 1 && scale > 0.5625) {
            if (longSide < 1664) {
                return 1;
            } else if (longSide >= 1664 && longSide < 4990) {
                return 2;
            } else if (longSide > 4990 && longSide < 10240) {
                return 4;
            } else {
                return longSide / 1280 == 0 ? 1 : longSide / 1280;
            }
        } else if (scale <= 0.5625 && scale > 0.5) {
            return longSide / 1280 == 0 ? 1 : longSide / 1280;
        } else {
            return (int) Math.ceil(longSide / (1280.0 / scale));
        }
    }


}